package com.naiterui.ehp.bs.doctor.dao.impl;

import com.naiterui.ehp.bp.domain.DrPhoneContact;
import com.naiterui.ehp.bs.doctor.dao.IDrPhoneContactDao;
import com.naiterui.ehp.bs.doctor.repository.IDrPhoneContactRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生手机联系人持久层实现
 *
 * @author Amber
 * @since 1.0.0
 */
@Component
public class AdDrPhoneContactDaoImpl  implements IDrPhoneContactDao {

    @Autowired
    private IDrPhoneContactRepository drPhoneContactRepository;

    @Override
    public void saveOrUpdate(DrPhoneContact phoneContact) {
        if(phoneContact.getId() == null){
            drPhoneContactRepository.save(phoneContact);
        } else {
            drPhoneContactRepository.update(phoneContact);
        }
    }

    @Override
    public List<DrPhoneContact> getContactsByDoctor(Long doctorId) {
        List<DrPhoneContact> contacts = drPhoneContactRepository.findByDoctorIdAndPatientIdIsNull(doctorId);
        return contacts;
    }

    @Override
    public Map<String, DrPhoneContact> getPhonesByDoctor(Long doctorId) {
        // 医生手机联系人
        List<DrPhoneContact> contacts = getContactsByDoctor(doctorId);
        // 手机号-联系人 map
        Map<String, DrPhoneContact> resultMap = new HashMap<>();

        // 处理map数据
        for (DrPhoneContact contact : contacts) {
            resultMap.put(contact.getPhone(), contact);
        }
        return resultMap;
    }

    @Override
    public DrPhoneContact getById(Long id) {
        return drPhoneContactRepository.get(id);
    }
}
